home *** CD-ROM | disk | FTP | other *** search
- //---------------------------------------------------------------------------
- #include <vcl\vcl.h>
- //
- // have to add this include for the PlaySound() function
- //
- #include <vcl\mmsystem.hpp>
- #pragma hdrstop
-
- #include "JJMain.h"
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- //
- // defines for the string resources
- //
- #define IDS_UP 101
- #define IDS_DOWN 102
-
- HINSTANCE hInst;
-
- TMainForm *MainForm;
- //---------------------------------------------------------------------------
- __fastcall TMainForm::TMainForm(TComponent* Owner)
- : TForm(Owner),
- done(false)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TMainForm::FormCreate(TObject *Sender)
- {
- // NOTE: The use of the hInst variable is necessary due to
- // problems with the C++Builder beta I was using at the time
- // this was written. The code listing in Day 9 of the book
- // should work as written.
-
- hInst = (HINSTANCE)HInstance;
- //
- // load and display the first bitmap
- //
- Image->Picture->Bitmap->
- LoadFromResourceName((int)hInst, "ID_BITMAP1");
- }
- //---------------------------------------------------------------------
- void __fastcall TMainForm::StartClick(TObject *Sender)
- {
- //
- // When the Start button is clicked the animation
- // loop starts. The bitmap resources are named
- // ID_BITMAP1 through ID_BITMAP5 so we'll start with
- // a string called "ID_BITMAP" and append the last
- // digit when needed.
- //
- String s = "ID_BITMAP";
- //
- // a buffer for the string resources
- //
- char buff[10];
- //
- // a flag to let us know when we're done
- //
- done = false;
- //
- // start the loop and keep looping until the 'Stop'
- // button is pressed
- //
- while (!done) {
- //
- // loop through the five bitmaps starting with
- // 1 and ending with 5
- //
- for (int i=1;i<6;i++) {
- //
- // append the value of 'i' to the end of the string
- // to build a string containing the resource name
- //
- String resName = s + String(i);
- //
- // call a class member function to display the bitmap
- //
- DrawImage(resName);
- }
- //
- // load the "Up" string resource using the WinAPI
- // function LoadString(), display the string,
- // and tell Windows to repaint the Label
- //
- LoadString(hInst, IDS_UP, buff, sizeof(buff));
- Label->Caption = buff;
- Label->Refresh();
- //
- // play the 'up' sound using the WinAPI function
- // PlaySound(), play it asynchronously
- //
- PlaySound("ID_WAVEUP",
- hInst, SND_ASYNC | SND_RESOURCE);
- //
- // pause for a moment at the top of the jump
- //
- Sleep(200);
- //
- // repeat all of the above except in reverse
- //
- for (int i=5;i>0;i--) {
- String resName = s + String(i);
- DrawImage(resName);
- }
- PlaySound("ID_WAVEDOWN",
- hInst, SND_ASYNC | SND_RESOURCE);
- LoadString(hInst, IDS_DOWN, buff, sizeof(buff));
- Label->Caption = buff;
- Label->Refresh();
- Sleep(200);
- }
- }
- //---------------------------------------------------------------------
- void __fastcall TMainForm::StopClick(TObject *Sender)
- {
- //
- // Stop button pressed, so tell the loop to stop executing
- //
- done = true;
- }
- //---------------------------------------------------------------------
- //
- // a class member function to display the bitmap
- //
- void
- TMainForm::DrawImage(String& name)
- {
- //
- // load the bitmap from a resource
- // using the name passed to us
- //
- Image->Picture->Bitmap->
- LoadFromResourceName((int)hInst, name);
- //
- // must pump the message loop so that Windows gets
- // a chance to display the bitmap
- //
- Application->ProcessMessages();
- //
- // take a short nap so the animation doesn't go too fast
- //
- Sleep(20);
- }
- //---------------------------------------------------------------------
-